package licode.unisop.auth.infrastructure.gateway;

import cn.hutool.core.bean.BeanUtil;
import licode.unisop.auth.client.info.IdentityContextIn;
import licode.unisop.auth.domain.user.gateway.AuthorGateway;
import licode.unisop.provider.info.SiIdentityContext;
import licode.unisop.provider.info.SiReturnCode;
import licode.unisop.provider.vo.SiIdentity;
import licode.unisop.realm.client.api.SiIdentityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 根据用户的认证信息对用户的真实性进行验证
 * 传入的参数为前端输入的用户认证信息和用户存储的认证信息
 * 如果信息一致，则通过
 *
 * @author licode
 */
@Service
public class AuthorGatewayAuth implements AuthorGateway {
    @Resource
    private SiIdentityService siIdentityService;

    @Override
    public boolean checkUser(IdentityContextIn context) {
        // 调用认证接口完成用户的认证
        SiReturnCode retCode = siIdentityService.authUser(SiIdentityContext.builder()
                .userId(context.getUser().getId())
                .realmId(context.getRealmId())
                .platformId(context.getPlatformId())
                .exist(BeanUtil.copyProperties(context.getExist(), SiIdentity.class))
                .target(BeanUtil.copyProperties(context.getTarget(), SiIdentity.class))
                .build());
        return retCode.getCode() == 0;
    }
}
